package cn.mystyle.service.account;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.mystyle.common.constant.DefaultURI;
import cn.mystyle.common.constant.ResultCodeJson;
import cn.mystyle.common.date.DateUtil;
import cn.mystyle.common.enums.AccountEnum;
import cn.mystyle.common.http.ServiceClient;
import cn.mystyle.common.response.ListResponse;
import cn.mystyle.model.account.Record;
import cn.mystyle.model.user.UserInfo;

/**
 * 账务业务类
 * 
 * @author  clark
 * @version  [版本号, 2017年7月11日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Service
public class AccountService
{
    
    /**
     * 默认统计
     * 
     * @param jsonData 查询条件
     * @param userParam 用户参数
     * @return String
     */
    public JSONObject statDefault(String jsonData, UserInfo userParam)
    {
        JSONObject statResult = ResultCodeJson.getSuccess();
        
        JSONObject param = JSONObject.parseObject(jsonData);
        Record recordParam = JSONObject.parseObject(param.getString("record"), Record.class);
        recordParam.setUserId(userParam.getId());
        
        int statType = param.getInteger("statType");
        
        // 调用账务原子服务
        ListResponse<Record> result = ServiceClient.callList(recordParam,
            DefaultURI.getRecordURIKey(DefaultURI.URI_QUERY),
            Record.getTypeReference());
        
        if (!result.isSuccess())
        {
            return JSONObject.parseObject(result.toString());
        }
        
        // 收入总金额
        BigDecimal incomeTotal = new BigDecimal(0);
        // 支出总金额
        BigDecimal costTotal = new BigDecimal(0);
        
        List<BigDecimal> income = null;
        List<BigDecimal> cost = null;
        if (statType == 0)
        {
            // ==> 按月统计
            // 按天将记录分成30份
            Date statDatetime = DateUtil.formatStringToDate(DateUtil.DATE_FORMAT_MONTH_BAR, recordParam.getLikeTime());
            Calendar statCalendar = Calendar.getInstance();
            statCalendar.setTime(statDatetime);
            // 本月总天数
            int dayOfMonth = statCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            
            income = getStatMonthTemplate(dayOfMonth);
            cost = getStatMonthTemplate(dayOfMonth);
            
            for (Record r : result.getResult())
            {
                Date datetime = DateUtil.formatStringToDate(DateUtil.DATE_FORMAT_DAY_BAR, r.getDateTime());
                Calendar dataCalendar = Calendar.getInstance();
                dataCalendar.setTime(datetime);
                int index = dataCalendar.get(Calendar.DAY_OF_MONTH) - 1;
                // 收入
                if (r.getType() == AccountEnum.TYPE_INCOME_1.getId())
                {
                    BigDecimal sumPrice = income.get(index);
                    BigDecimal recordTotalPrice = new BigDecimal(r.getTotalPrice());
                    incomeTotal = incomeTotal.add(recordTotalPrice);
                    income.set(index, sumPrice.add(recordTotalPrice));
                }
                else
                {
                    // 支出
                    BigDecimal sumPrice = cost.get(index);
                    BigDecimal recordTotalPrice = new BigDecimal(r.getTotalPrice());
                    costTotal = costTotal.add(recordTotalPrice);
                    cost.set(index, sumPrice.add(recordTotalPrice));
                }
                
            }
        }
        else
        {
            // ==> 按年统计
            // 按月将记录分成12份
            income = getStatYearTemplate();
            cost = getStatYearTemplate();
            
            for (Record r : result.getResult())
            {
                Date datetime = DateUtil.formatStringToDate(DateUtil.DATE_FORMAT_DAY_BAR, r.getDateTime());
                Calendar dataCalendar = Calendar.getInstance();
                dataCalendar.setTime(datetime);
                int index = dataCalendar.get(Calendar.MONTH);
                // 收入
                if (r.getType() == AccountEnum.TYPE_INCOME_1.getId())
                {
                    BigDecimal sumPrice = income.get(index);
                    BigDecimal recordTotalPrice = new BigDecimal(r.getTotalPrice());
                    incomeTotal = incomeTotal.add(recordTotalPrice);
                    income.set(index, sumPrice.add(recordTotalPrice));
                }
                else
                {
                    // 支出
                    BigDecimal sumPrice = cost.get(index);
                    BigDecimal recordTotalPrice = new BigDecimal(r.getTotalPrice());
                    costTotal = costTotal.add(recordTotalPrice);
                    cost.set(index, sumPrice.add(recordTotalPrice));
                }
            }
        }
        
        // 返回结果
        JSONArray pieStat = new JSONArray();
        pieStat.add(incomeTotal);
        pieStat.add(costTotal);
        statResult.put("pieStat", pieStat);
        statResult.put("income", income);
        statResult.put("cost", cost);
        
        return statResult;
    }
    
    /*private JSONObject getStatDefault(int statType)
    {
        JSONObject resultDefault = new JSONObject();
        if (statType == 0)
        {
            // ==> 按月统计
            JSONObject monthDefault = new JSONObject();
            // 饼图
            JSONArray pie = new JSONArray();
            pie.add(0);// 支出
            pie.add(0);// 收入
            monthDefault.put("pie", pie);
            
            // 条形统计图
            JSONArray bar = new JSONArray();
            bar.add(getStatMonthTemplate());// 支出
            bar.add(getStatMonthTemplate());// 收入
            monthDefault.put("bar", bar);
            
            resultDefault.put("monthStat", monthDefault);
        }
        else
        {
            // ==> 按年统计
            JSONObject yearDefault = new JSONObject();
            // 饼图
            JSONArray pie = new JSONArray();
            pie.add(0);// 支出
            pie.add(0);// 收入
            yearDefault.put("pie", pie);
            
            // 条形统计图
            JSONArray bar = new JSONArray();
            bar.add(getStatYearTemplate());// 支出
            bar.add(getStatYearTemplate());// 收入
            yearDefault.put("line", bar);
            
            resultDefault.put("yearStat", yearDefault);
        }
        
        return resultDefault;
    }*/
    
    /** 
     * 获取当前月模板
     * 
     * @param dayOfMonth 当前月共多少天
     * @return List<BigDecimal>
     * @see [类、类#方法、类#成员]
     */
    private List<BigDecimal> getStatMonthTemplate(int dayOfMonth)
    {
        List<BigDecimal> days = new ArrayList<BigDecimal>();
        for (int i = 0; i < dayOfMonth; i++)
        {
            days.add(new BigDecimal(0));
        }
        return days;
    }
    
    /** 
     * 获取年度模板
     * @return List<BigDecimal>
     * @see [类、类#方法、类#成员]
     */
    private List<BigDecimal> getStatYearTemplate()
    {
        List<BigDecimal> months = new ArrayList<BigDecimal>();
        for (int i = 0; i < 12; i++)
        {
            months.add(new BigDecimal(0));
        }
        return months;
    }
    
}
